home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_362 / puzz / source / main.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  227 lines

  1. /*    Declarations for CBACK    */
  2. extern BPTR _Backstdout;        /*    std output when run in background    */
  3. long _BackGroundIO = 1;            /*    Flag to tell it we want to do I/O    */
  4. long _stack = 4000;                /*    Amount of stack space task needs    */
  5. char *_procname = "PUZZ";        /*    The name of the task to create        */
  6. long _priority = 1;                /*    The priority to run us at            */
  7.  
  8. struct IntuitionBase *IntuitionBase;
  9. struct GfxBase *GfxBase;
  10.  
  11. int sqrsacross,sqrsdown;
  12. unsigned char start [MAXACROSS] [MAXDOWN];
  13. unsigned char goal  [MAXACROSS] [MAXDOWN];
  14. unsigned char grid    [MAXACROSS] [MAXDOWN];
  15. unsigned char movelist [MAXPIECES] [4];
  16.  
  17. int topleftx,toplefty;
  18. int sqrwidth,sqrheight;
  19. int slide;
  20.  
  21. int moves;
  22. int dmapwidth,dmapheight;
  23. int audio;
  24.  
  25. BitMapHeader bmhd;
  26.  
  27. struct RastPort *dbuff;
  28.  
  29. struct TextAttr MyFont =
  30.     {
  31.     (STRPTR)"topaz.font",
  32.     TOPAZ_EIGHTY, 0, 0
  33.     };
  34.  
  35. extern struct Screen *loadpuzzle(int, char **);
  36. extern struct RastPort *getbuffs(int,int);
  37. extern int get_audio(void);
  38. extern void StopVoices(void);
  39. extern void freebuffs(int,int);
  40. extern void Expand(struct Screen *, BitMapHeader *);
  41. extern void saveundermenu(struct RastPort *);
  42. extern void drawovermenu(struct RastPort *);
  43. extern void print(char *s);
  44. extern struct Menu MyMenu;
  45.  
  46. void cleanup(int,struct Screen *, struct Window *);
  47. void startpuzzle(struct Screen *,struct Window *);
  48. extern void clicked(struct RastPort *,int,int);
  49.  
  50. /*    following stuff so we can work from CLI or WorkBench    */
  51.  
  52. int runningfromcli;
  53.  
  54. #define MAXARGS 32
  55.  
  56. /*    Using _main() and cback.o avoids the stupid console
  57.     window opening when we are run from the Workbench
  58. */
  59.  
  60. void _main(char *cmd)
  61.     {
  62.     int argc = 0;
  63.     char *argv[MAXARGS];
  64.     
  65.     struct Screen *screen;
  66.     struct Window *window;
  67.     struct NewWindow nw;
  68.     struct IntuiMessage *msg;
  69.  
  70.     /*    This code fragment gets argc and argv as if from main()        */
  71.     if (cmd && *cmd)
  72.         {
  73.         argv[argc] = strtok(cmd,", \n");
  74.         while(argv[argc])
  75.             argv[++argc] = strtok(NULL,", \n");
  76.         }
  77.  
  78.     runningfromcli = (argc == 0 ? 0 : 1);
  79.  
  80.     if (runningfromcli)        /*    Output my banner.    */
  81.         {
  82.         print("\n\2330;33mPUZZ  V1.0\2330m  by\t\2331mMartin Round.\2330m\n");
  83.         print("\t\t17, Naseby Drive, Halesowen, West Mids.\n");
  84.         print("\t\tB63 1HJ  England.  Contact me through\n");
  85.         print("\t\tthe Plug 'Ole B.B.  (021-472-0256) or\n");
  86.         print("\t\tAMLINK B.B.  (021-778-5868).\n\n");
  87.         }
  88.     
  89.     IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  90.     GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0L);
  91.  
  92.     screen=loadpuzzle(argc,argv);    /*    loads puzzle data and then        */
  93.                                     /*    reads pic into compr. buffer    */
  94.                                       /*    returns pointer to opened        */
  95.                                     /*    screen, or NULL for failure        */
  96.  
  97.     SetTaskPri(FindTask(NULL),0);    /*    now run at standard priority    */
  98.     
  99.     dmapwidth = sqrsacross * sqrwidth;
  100.     dmapheight = sqrsdown * sqrheight;
  101.  
  102.     if (screen == NULL)
  103.         {
  104.         cleanup(0,screen,(window = NULL));
  105.         exit(0L);
  106.         }
  107.  
  108.     /*    get buffers for saving 'menu' part of pic and double-buffering    */
  109.  
  110.     if ((dbuff = getbuffs(dmapwidth,dmapheight)) == NULL)
  111.         {
  112.         cleanup(0,screen,(window = NULL));
  113.         exit(0L);
  114.         }
  115.  
  116.     /*    attempt to get 4 audio channels - if we can't then we will
  117.         still run, but without sounds.
  118.     */
  119.     audio = get_audio();
  120.     
  121.     setmem(&nw,sizeof(nw),0);
  122.     nw.IDCMPFlags=MOUSEBUTTONS|MENUPICK;
  123.     nw.Flags=NOCAREREFRESH|BORDERLESS|ACTIVATE;
  124.     nw.Screen=screen;
  125.     nw.Type=CUSTOMSCREEN;
  126.     nw.Height=bmhd.h; nw.Width=bmhd.w;
  127.     nw.DetailPen = 1;
  128.     nw.BlockPen = 0;
  129.  
  130.     if (!(window=OpenWindow(&nw)))
  131.         { 
  132.         cleanup (1,screen,window);
  133.         exit(0L);
  134.         }
  135.   
  136.     startpuzzle(screen,window);
  137.  
  138.     SetMenuStrip(window, &MyMenu);
  139.  
  140.     for (;;)    /*    FOREVER    */
  141.         {
  142.         Wait(1 << window->UserPort->mp_SigBit);        /*    sleep    */
  143.  
  144.         while ((msg = (struct IntuiMessage *)GetMsg(window->UserPort)) != NULL)
  145.             {
  146.             switch (msg->Class)
  147.                 {
  148.                 case MENUPICK:
  149.                     if (MENUNUM(msg->Code) == 0)        /*    Start    */
  150.                         {
  151.                         startpuzzle(screen,window);
  152.                         ReplyMsg((struct Message *)msg);
  153.                         }
  154.                     else if (MENUNUM(msg->Code) == 1)    /*    Quit    */
  155.                         {
  156.                         ReplyMsg((struct Message *)msg);
  157.                         cleanup (3,screen,window);
  158.                         exit (0L);
  159.                         }
  160.                     else
  161.                         {
  162.                         drawovermenu(window->RPort);
  163.                         ReplyMsg((struct Message *)msg);
  164.                         }
  165.                     break;
  166.  
  167.                 case MOUSEBUTTONS:
  168.                     if (msg->Code == SELECTDOWN)
  169.                         {
  170.                         drawovermenu(window->RPort);
  171.                         clicked(window->RPort,msg->MouseX,msg->MouseY);
  172.                         saveundermenu(window->RPort);
  173.                         }
  174.                     ReplyMsg((struct Message *)msg);
  175.                     break;
  176.                     
  177.                 default:
  178.                     ReplyMsg((struct Message *)msg);
  179.                     break;
  180.                 }
  181.             }
  182.         }
  183.  
  184.     }    /*    end of main    */
  185.  
  186. void cleanup(int level,struct Screen *screen,struct Window *window)
  187.     {
  188.     switch (level)
  189.         {
  190.         case 3:
  191.             if (audio)
  192.                 {
  193.                 StopVoices();
  194.                 clear_audio(0);
  195.                 }
  196.         case 2:
  197.             ClearMenuStrip(window);
  198.             freebuffs(dmapwidth,dmapheight);
  199.             CloseWindow(window);
  200.         case 1:
  201.             CloseScreen(screen);
  202.         case 0:
  203.             CloseLibrary((struct Library *) IntuitionBase);
  204.             CloseLibrary((struct Library *) GfxBase);
  205.             if (_Backstdout)  Close(_Backstdout);
  206.             break;
  207.         }
  208.     }
  209.  
  210. void startpuzzle(struct Screen *screen,struct Window *window)
  211.     {
  212.     int i,j;
  213.     
  214.     Expand(screen,&bmhd);        /*    actually draw the piccy    */
  215.     saveundermenu(window->RPort);
  216.     
  217.     for (j=0; j<sqrsdown; j++)
  218.         for (i=0; i<sqrsacross; i++)
  219.             grid [i] [j] = start [i] [j];
  220.  
  221.     moves = 0;
  222.     
  223.     for (i=0; i<MAXPIECES; i++)
  224.         for (j=0; j<4; j++)
  225.             movelist [i] [j] = j+1;
  226.     }
  227.